package com.jack.leetcode.eachProblem07;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class 戳气球 {

    public static void main(String[] args) {
        int[] a = {3, 1, 5, 8};
        System.out.println(maxCoins(a));
    }

    public static int maxCoins(int[] nums) {
        if (nums == null) {
            return 0;
        }
        int[] newNum = new int[nums.length + 2];
        newNum[0] = 1;
        newNum[nums.length + 1] = 1;
        System.arraycopy(nums, 0, newNum, 1, nums.length);
        int n = newNum.length;
        int[][] dp = new int[n][n];
        for (int i = n - 2; i >= 0; i--) {
            for (int j = i + 2; j < n; j++) {
                int max = 0;
                for (int k = i + 1; k < j; k++) {
                    int temp = dp[i][k] + dp[k][j] + newNum[i] * newNum[k] * newNum[j];
                    if (temp > max) {
                        max = temp;
                    }
                }
                dp[i][j] = max;
            }
        }
        return dp[0][n - 1];
    }

}
